<?php
namespace app\api\controller;
use app\admin\model\SecretaryDynamic;
use app\admin\model\Comment;
use think\Db;
use think\Validate;
class Secretary extends Common{
    public function _initialize()
    {
        // todo...
        parent::_initialize();
    }

    public function __construct() //Db::connect('database_app');
    {
        $this->db_app = Db::connect('database_app');
    }

    // 测试接口
    public function testApi() {

        $this->outData['data'] = array("PHP","JAVA","MYSQL");
        $this->outData['code'] = 1;
        $this->outData['message'] = "操作成功";

		//dump($this->outData);
        return json($this->outData);
    }

    //云秘书最新动态
    public function recentDynamic()
    {
        $dynamic = new SecretaryDynamic;
        $info = $dynamic -> field('dy_id,dy_title,dy_pic,dy_des,dy_time') -> where('dy_isdel','1') -> where('dy_platformid','neq','') -> order('dy_time desc') -> find();
        if($info){
            $this->outData['data'] = $info;
            $this->outData['code'] = 1;
            $this->outData['message'] = "操作成功";
            return $this->outData;
        }else{
            $this->outData['code'] = 3;
            $this->outData['message'] = "暂无数据";
            return $this->outData;
        }
    }

    //云秘书动态详情 参数动态Id   dy_id  用户id  userid
    public function recentDynamicDetail()
    {
        $dy_id = input('dy_id');
        $userid = input('userid');
        Db::name('secretary_dynamic')->where('dy_id',$dy_id)->setInc('dy_liul');
        $dynamic = new SecretaryDynamic;
        $info = $dynamic -> where('dy_id',$dy_id) -> find();
        //return $this->outData['sql'] = $dynamic->getLastSql();
        if($info){
            //点赞状态
            if($info['dy_dianzuserid']){
                $useridArr = explode(',', $info['dy_dianzuserid']);
                $result = array_search($userid, $useridArr);//返回键值
                if($result !== false){
                    $info['dianzStatus'] = '已点赞';
                }else{
                    $info['dianzStatus'] = '未点赞';
                }
            }else{
                $info['dianzStatus'] = '未点赞';
            }
            unset($info['dy_dianzuserid']);
            $this->outData['data'] = $info;
            $this->outData['code'] = 1;
            $this->outData['message'] = "操作成功";
            return $this->outData;
        }else{
            $this->outData['code'] = 2;
            $this->outData['message'] = "操作失败";
            return $this->outData;
        }
        
    }
    //云秘书历史动态
    public function historyDynamic()
    {
    	$dynamic = new SecretaryDynamic;
        $info = $dynamic -> field('dy_id,dy_title,dy_pic,dy_des,dy_time') -> where('dy_isdel','1') ->where('dy_platformid','neq','') -> order('dy_time desc') -> select();
        if($info){
            $this->outData['data'] = $info;
            $this->outData['code'] = 1;
            $this->outData['message'] = "操作成功";
            return $this->outData;
        }else{
            $this->outData['code'] = 3;
            $this->outData['message'] = "暂无数据";
            return $this->outData;
        }
    }

    //我有话说接受并保存数据 参数 用户id user_id 主题 mes_title 内容 mes_content 照片 pic
    public function message()
    {
    	//接受数据
    	$userid = input('user_id');
        $info = $this->db_app->table('customer') -> where('Id',$userid) -> find();
        $data['mes_nick'] = $info['NickName'];
        $data['mes_userid'] = $userid;
        $data['mes_title'] = input('mes_title');
        $data['mes_content'] = input('mes_content');
        $data['mes_time'] = time();
        $data['mes_replyhead'] = 'http://media1.yunlutong.com/business/20171020/fb6a980c490ae952e208fadec6e5bab4.png';
        $data['mes_pic'] = input('pic/a');
        if(is_array($data['mes_pic'])){
            foreach($data['mes_pic'] as $k => $v){
                $data['mes_pic'][$k] = str_replace('\/', '/', $v);
            }
            $data['mes_pic'] = implode(',', $data['mes_pic']);
        }
        $validate = new Validate([
            'mes_title'  => 'require|max:10',
            'mes_content' => 'require|max:500'
        ]);
        if (!$validate->check($data)) {
            $this->outData['code'] = 4;
            $this->outData['message'] = $validate->getError();
            return $this->outData;
        }
        $res = Db::name('secretary_message') -> insert($data);
    	if($res){
		    $this->outData['code'] = 1;
		    $this->outData['message'] = "操作成功";
		    return $this->outData;
    	}else{
		    $this->outData['code'] = 2;
		    $this->outData['message'] = "操作失败";
		    return $this->outData;
    	}
    }

    //留言列表 参数  用户id  userid  搜索内容  keyword
    public function message_list()
    {
        $userid = input('userid');
        // 是否有参数传入
        if (input('?param.keyword')) {
            $keyword = input('param.keyword');
        } else {
            $keyword = '';
        }
        $info = $this->db_app->table('genericattribute')
                    ->alias('g')
                    ->join('yltmg.ylt_secretary_message m','g.EntityId = m.mes_userid')
                    ->field('m.mes_id,m.mes_title,m.mes_content,m.mes_time,m.mes_replyhead,m.mes_replycontent,m.mes_replystatus,m.mes_replytime,g.Value as headPic')
                    ->where('g.Key','Avatar')
                    ->where('mes_userid',$userid)
                    ->where('mes_title|mes_content','like','%'.$keyword.'%')
                    ->order('mes_time desc')
                    ->select();
        if($info){
            $this->outData['data'] = $info;
            $this->outData['code'] = 1;
            $this->outData['message'] = "操作成功";
            return $this->outData;
        }else{
            $this->outData['code'] = 3;
            $this->outData['message'] = "暂无数据";
            return $this->outData;
        }
    }

    //获取留言详情  参数 留言id  mes_id
    public function messageDetail()
    {
        $mes_id = input('mes_id');
        $info = $this->db_app->table('genericattribute')
                    ->alias('g')
                    ->join('yltmg.ylt_secretary_message m','g.EntityId = m.mes_userid')
                    ->field('m.mes_id,m.mes_title,m.mes_content,m.mes_time,m.mes_pic,m.mes_replynick,m.mes_replycontent,m.mes_replypic,m.mes_replystatus,g.Value as headPic')
                    ->where('g.Key','Avatar')
                    ->where('mes_id',$mes_id)
                    ->order('mes_time desc')
                    ->find();
        if($info){
            if($info['mes_replycontent'] && $info['mes_replystatus'] == 0){
                Db::name('secretary_message') -> where('mes_id',$mes_id) -> update(['mes_replystatus' => 1]);
            }
            $this->outData['data'] = $info;
            $this->outData['code'] = 1;
            $this->outData['message'] = "操作成功";
            return $this->outData;
        }else{
            $this->outData['code'] = 2;
            $this->outData['message'] = "操作失败";
            return $this->outData;
        }
    }

    //评论 参数 评论模块类型 co_type 模块id  type_id 内容 co_content 用户id co_userid
    public function pingl()
    {
        //获取数据
        $userid = input('co_userid');
        $data = input('post.');
        $data['co_time'] = date('Y-m-d H:i:s',time());
        //获取用户头像和昵称
        $info = $this->db_app->table('genericattribute')
                    -> alias('g')
                    -> join('yunlutong.customer c','g.EntityId = c.Id')
                    -> field('c.NickName,g.Value as headPic')
                    -> where('g.EntityId',$userid)
                    -> where('g.Key','Avatar')
                    -> select();
        //$info = $this->db_app->table('genericattribute') -> where('EntityId',$userid) -> where('Key','NickName') -> find();
        $data['co_userpic'] = $info[0]['headPic'];
        $data['co_usernick'] = $info[0]['NickName'];
        /*$type = input('type');
        $content = input('content');
        $id = input('id');*/
        $res = Db::name('comment') -> insert($data);
        if($res){
            //评论成功，评论数加1
            switch ($data['co_type']) {
                case '商学院动态':
                    Db::name('business_college_dynamic')->where('dy_id',$data['type_id'])->setInc('dy_pingl');
                    break;
                case '商学院课程':
                    Db::name('business_college_course')->where('course_id',$data['type_id'])->setInc('course_pingl');
                    break;
                case '商学院素材':
                    Db::name('business_college_material')->where('mat_id',$data['type_id'])->setInc('mat_pingl');
                    break;
                case '云秘书动态':
                    Db::name('secretary_dynamic')->where('dy_id',$data['type_id'])->setInc('dy_pingl');
                    break;
                case '消息平台动态':
                    Db::name('business_college_dynamic')->where('dy_id',$data['type_id'])->setInc('dy_pingl');
                    break;
                /*default:
                    # code...
                    break;*/
            }
            $this->outData['code'] = 1;
            $this->outData['message'] = "操作成功";
            return $this->outData;
        }else{
            $this->outData['code'] = 2;
            $this->outData['message'] = "操作失败";
            return $this->outData;
        }

    }

    //评论回复 参数 回复的评论id  co_id 回复人的用户id  userid 被回复人的昵称 respondentsnick  没有则默认为空字符串''  回复内容  content
    public function reply()
    {
        $data['co_id'] = input('co_id');
        $data['respondentsnick'] = input('respondentsnick');
        $data['content'] = input('content');
        $userid = input('userid');
        $data['replyuserid'] = $userid;
        $info = $this->db_app->table('customer') -> where('Id',$userid) -> find();
        $data['replynick'] = $info['NickName'];
        $data['time'] = date('Y-m-d H:i:s',time());
        $res = Db::name('reply') -> insert($data);
        if($res){
            $this->outData['code'] = 1;
            $this->outData['message'] = "操作成功";
            return $this->outData;
        }else{
            $this->outData['code'] = 2;
            $this->outData['message'] = "操作失败";
            return $this->outData;
        }

    }

    //动态评论列表 参数 评论模块 co_type(包括'云秘书动态','商学院素材','商学院课程','消息平台动态','商学院动态') 模块id  type_id(当前模块的主键id) 用户id userid 当前页 page 每页显示条数 listRows
    public function commentList()
    {
        $co_type = input('co_type');
        $type_id = input('type_id');
        $userid = input('userid');
        $page = input('page');
        $listRows = input('listRows');
        $data = Db::name('comment')
            ->where('co_isdel','1')
            ->where('co_type',$co_type)
            ->where('type_id',$type_id)
            ->order('co_dianz desc,co_time asc')
            ->page($page,$listRows)
            ->select();
        if($data){
            //回复列表
            $replyData = array();
            foreach($data as $k => $v){
                $data[$k]['replyNum'] = Db::name('reply') -> where('co_id',$v['co_id']) -> count();
                $replyInfo = Db::name('reply')
                                ->where('isdel','1')
                                ->where('co_id',$v['co_id'])
                                ->limit(2)
                                ->select();
                $replyData = array_merge($replyData,$replyInfo);
                //点赞状态
                if($v['co_dianzuserid']){
                    $useridArr = explode(',', $v['co_dianzuserid']);
                    $result = array_search($userid, $useridArr);//返回键值
                    if($result !== false){
                        $data[$k]['dianzStatus'] = '已点赞';
                    }else{
                        $data[$k]['dianzStatus'] = '未点赞';
                    }
                }else{
                    $data[$k]['dianzStatus'] = '未点赞';
                }
                unset($data[$k]['co_dianzuserid']);
            }
            $info['pingl'] = $data;
            $info['reply'] = $replyData;
            $this->outData['data'] = $info;
            $this->outData['code'] = 1;
            $this->outData['message'] = "操作成功";
            return $this->outData;
        }else{
            $this->outData['code'] = 3;
            $this->outData['message'] = "暂无数据";
            return $this->outData;
        }
    }

    //单个评论的回复列表  参数  评论id co_id  当前页 page 每页显示条数 listRows
    public function replyList()
    {
        $co_id = input('co_id');
        $info = Db::name('reply')
            ->where('co_id',$co_id)
            ->order('time asc')
            ->select();
        if($info){
            $this->outData['data'] = $info;
            $this->outData['code'] = 1;
            $this->outData['message'] = "操作成功";
            return $this->outData;
        }else{
            $this->outData['code'] = 3;
            $this->outData['message'] = "暂无数据";
            return $this->outData;
        }
    }
    
    //自己评论删除 参数co_id
    public function commentDelete()
    {
        $id = input('co_id');
        $res = Db::name('comment')->where('co_id',$id)->update(['co_isdel' => '0']);
        if($res){
            $this->outData['code'] = 1;
            $this->outData['message'] = "操作成功";
            return $this->outData;
        }else{
            $this->outData['code'] = 2;
            $this->outData['message'] = "操作失败";
            return $this->outData;
        }
    }

    //自己回复删除 参数回复id
    public function replyDelete()
    {
        $id = input('id');
        $res = Db::name('reply')->where('id',$id)->setField('isdel','0');
        if($res){
            $this->outData['code'] = 1;
            $this->outData['message'] = "操作成功";
            return $this->outData;
        }else{
            $this->outData['code'] = 2;
            $this->outData['message'] = "操作失败";
            return $this->outData;
        }
    }

    //评论点赞自增 评论co_id 点赞用户id  userid
    public function commentDianz()
    {
        $id = input('co_id');
        $userid = input('userid');
        $info = Db::name('comment') -> where('co_id',$id) -> find();
        if($info['co_dianzuserid']){
            $useridArr = explode(',', $info['co_dianzuserid']);
            $result = array_search($userid, $useridArr);//返回键值
            if($result !== false){//已点赞,取消点赞
                $res = Db::name('comment')->where('co_id',$id)->setDec('co_dianz');
                unset($useridArr[$result]);
            }else{//未点赞，点赞
                $res = Db::name('comment')->where('co_id',$id)->setInc('co_dianz');
                $useridArr[] = $userid;
            }
        }else{
            $res = Db::name('comment')->where('co_id',$id)->setInc('co_dianz');
            $useridArr[] = $userid;
        }
        $useridString = implode(',', $useridArr);
        Db::name('comment') -> where('co_id',$id) -> update(['co_dianzuserid' => $useridString]);
        if($res){
            $this->outData['code'] = 1;
            $this->outData['message'] = "操作成功";
            return $this->outData;
        }else{
            $this->outData['code'] = 2;
            $this->outData['message'] = "操作失败";
            return $this->outData;
        }
    }

    //点赞,取消点赞 动态id  dy_id  用户id  userid
    public function secretaryDianzNum()
    {
        $dy_id = input('dy_id');
        $userid = input('userid');
        $info = Db::name('secretary_dynamic') -> where('dy_id',$dy_id) -> find();
        if($info['dy_dianzuserid']){
            $useridArr = explode(',', $info['dy_dianzuserid']);
            $result = array_search($userid, $useridArr);//返回键值
            if($result !== false){//已点赞,取消点赞
                $res = Db::name('secretary_dynamic')->where('dy_id',$dy_id)->setDec('dy_dianz');
                unset($useridArr[$result]);
            }else{//未点赞，点赞
                $res = Db::name('secretary_dynamic')->where('dy_id',$dy_id)->setInc('dy_dianz');
                $useridArr[] = $userid;
            }
        }else{
            $res = Db::name('secretary_dynamic')->where('dy_id',$dy_id)->setInc('dy_dianz');
            $useridArr[] = $userid;
        }
        $useridString = implode(',', $useridArr);
        Db::name('secretary_dynamic') -> where('dy_id',$dy_id) -> update(['dy_dianzuserid' => $useridString]);
        if($res){
            $this->outData['code'] = 1;
            $this->outData['message'] = "操作成功";
            return $this->outData;
        }else{
            $this->outData['code'] = 2;
            $this->outData['message'] = "操作失败";
            return $this->outData;
        }
    }



}